package com.stars.home.vmodel

import android.app.Application
import android.text.Editable
import android.text.TextUtils
import android.widget.FrameLayout
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.BR
import com.stars.home.activity.AuthDriverActivity
import com.stars.home.activity.DriverDetailsActivity
import com.stars.home.activity.GrabDetailsActivity
import com.stars.home.config.ConfigRx
import com.stars.home.inte.ChoiceData
import com.stars.home.model.IndexTop
import com.stars.home.model.User
import com.stars.home.utils.PopuTipsUtils
import com.stars.home.utils.net.NetEngine
import com.stars.home.view.SimpleTextWatcher
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.net.IResp
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

class HomeDriverVModel(application: Application) : BRViewModel<BRRepository>(application) {

    var lat = ""
    var lng = ""
    var start_time = MutableLiveData<String>()
    var end_time = MutableLiveData<String>()
    var start_city_name = MutableLiveData<String>()
    var end_city_name = MutableLiveData<String>()

    var bean = MutableLiveData<IndexTop>()

    var fl: FrameLayout? = null


    @Bindable
    open var k: String? = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.k)
        }


    var oldText = ""
    var textWatcher = object : SimpleTextWatcher() {
        override fun afterTextChanged(s: Editable?) {
            super.afterTextChanged(s)
            var content = s.toString()
            if (!TextUtils.isEmpty(oldText) && content == oldText) {
                return
            }
            oldText = content
            //刷新
            firstReq()


        }

    }


    fun showTips(payment: Int): String {//0全款,1=到付,2=付定金
        if (payment == 0) {
            return " (全款) "
        }
        if (payment == 1) {
            return " (到付) "
        }
        return " (付定金) "
    }


    fun getHeadInfo() {//banner信息

        viewModelScope.launch {
            var it = repository().lreqTo { NetEngine.service.indexTop() }
            if (it.ok()) {
                bean.value = it.data
            }
        }

    }


    fun toReceOrder(order_id: String?, position: Int?) {
        //到接单界面
        UiSwitch.bundle(repository().context, GrabDetailsActivity::class.java,
                B().putString("order_id", order_id)
                        .putString("position", "" + position)
                        .ok())


    }



    fun toGrabeOrder(order_id: String?, position: Int?) {
        //先去判断该司机有没有认证







    }






    override fun apiFunc(): suspend () -> IResp<*> {
        return {
            NetEngine.service.orderListHome(lat, lng, start_time.value, end_time.value, start_city_name.value, end_city_name.value, "10", repository().page)
        }
    }


}
